#!/bin/bash

repository=${OCROREP:-ssh://define.me/ocropus}
dirs="iulib llpy ocrofst ocrojni ocrolseg ocropy ocrorast pyopenfst"
default_download=${OCRODOWNLOAD:-http://www-user.rhrk.uni-kl.de/~tmb}
default_cmodels="uw3unlv-240-4-60-g2.cmodel uw3unlv-240-8-40-nogeo.cmodel space.model space2.model default.fst none.fst case.fst" 
datadir=/usr/local/share/ocropus

function doHg {
    for dir in $dirs; do
        echo "=== cd $dir; hg $*"
            (cd $dir && hg "$@") ||
                (echo $dir: error; exit 1)
    done
}

function doClone {
    for dir in $dirs; do
        echo === cloning $dir
            hg clone $repository/$dir || 
                (echo $dir: error cloning; exit 1)
    done
}

function doPull {
    for dir in $dirs; do
        echo === pulling $dir
            (cd $dir && hg pull $repository/$dir && hg update) || 
                (echo $dir: error pulling; exit 1)
    done
}

function doPush {
    for dir in $dirs; do
        echo === pushing $dir
        (cd $dir && hg push $repository/$dir) || 
            (echo $dir: error pushing; exit 1)
    done
}

function doCommit {
    for dir in $dirs; do
        echo === committing $dir
            (cd $dir && hg commit "$@") || 
                (echo $dir: error committing; exit 1)
    done
}

function doPackages {
    if [ "$1" == "yum" ]; then
        sudo yum install \
            make cmake automake gcc gcc-c++ kernel-devel byacc \
            scons mercurial ImageMagick \
            libpng-devel libjpeg-turbo-devel libtiff-devel giflib-devel \
            SDL-devel SDL_gfx-devel SDL_image-devel\
            hdf5-devel ffmpeg-devel \
            blas-devel gsl-devel \
            lapack-devel atlas-devel \
            python-matplotlib python-matplotlib-doc python-imaging-devel \
            python-devel numpy scipy\
            python-tables swig \
            libsqlite3x-devel \
            tesseract-devel \
            ipython curl
    else
        sudo apt-get install \
            build-essential imagemagick scons mercurial \
            libpng12-dev libjpeg62-dev libtiff4-dev libgif-dev \
            libsdl-gfx1.2-dev libsdl-image1.2-dev libsdl1.2-dev \
            libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
            libavfilter-dev \
            libpng12-dev libjpeg62-dev libtiff4-dev libgif-dev \
            libsdl-gfx1.2-dev libsdl-image1.2-dev libsdl1.2-dev \
            libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
            libavfilter-dev \
            libgsl0-dev libblas-dev \
            libsqlite3-dev\
            liblapack-dev libatlas-base-dev \
            python-dev swig1.3 zlib1g-dev \
            python-scipy python-matplotlib python-matplotlib-data \
            python-tables \
            ipython curl
            # tesseract-ocr tesseract-ocr-dev
            # tesseract-ocr-dev libleptonica-dev
    fi
}

function doCleanup {
    rm -rf flann*/build
    rm -rf flann*/src/python/build
    rm -rf iuprlab/build
    rm -rf ocrofst/build
    rm -rf ocrolseg/build
    rm -rf ocropy/build
    rm -rf ocrorast/build
    rm -rf pyopenfst/build

    for dir in $dirs; do
        echo $dir
        (cd $dir && python setup.py clean)
        (cd $dir && test -f SConstruct && scons -c)
        (cd $dir && test -f Makefile && make clean)
    done
}

function doInstall {
    set -o errexit

    echo please sudo now so that installs work
    sudo id

    # compile iulib

    (
    cd iulib
    scons -c
    sudo sh uninstall
    doPackages $1
    scons -j 4 sdl=1
    sudo scons -j 4 sdl=1 install
    cd pyswig
    python setup.py build_ext
    sudo python setup.py install
    )

    # compile ocrolseg

    (
    cd ocrolseg
    make
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocrorast

    (
    cd ocrorast
    make
    python setup.py build_ext
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocropy

    (
    cd ocropy
    sudo python setup.py install
    cd ..
    date;
    )

    # compile llpy

    (
    cd llpy
    sudo python setup.py install
    cd ..
    date;
    )

    # compile ocrofst 

    (
    cd ocrofst
    (cd ocrofstll && scons && sudo scons install)
    python setup.py build_ext
    sudo python setup.py install
    cd ..
    date;
    )

    # compile openfst 

    (
    rm -rf openfst-1.1
    test -f DIST/openfst-1.1.tar.gz && tar -zxvf DIST/openfst-1.1.tar.gz 
    cd openfst-1.1
    ./configure
    make -j 4
    sudo make install
    cd ..
    date;
    )

    # compile Python bindings for openfst

    (
    cd pyopenfst
    make
    cd ..
    date;
    )

    # optionally compile FLANN

    (
    sudo rm -rf flann-1.7.1-src
    unzip DIST/flann-1.7.1-src.zip
    cd flann-1.7.1-src
    rm -rf build
    mkdir build
    cd build
    cmake .. -DBUILD_MATLAB_BINDINGS=OFF
    make
    sudo make install
    )

    # last messages to the user

    (echo $LD_LIBRARY_PATH | grep /usr/local/lib) || {
        echo
        echo IMPORTANT
        echo
        echo You must add /usr/local/lib/ to your LD_LIBRARY_PATH variable.
        echo and add /usr/local/bin to your PATH variable.
        echo
    }
}

function doDownload {
    (
    if [ ! -d $datadir ]; then
        mkdir -p $datadir
    fi
    cd $datadir || die "can't cd to $datadir"
    rm -f default.cmodel
    for m in $default_cmodels; do
        if test -f $m; then continue; fi
        curl $default_download/$m.bz2 | bunzip2 > $m || rm -f $m
        test -e default.cmodel || ln -s $m default.cmodel || true
    done
    )

}

function doUninstall {
    prefix=/usr/local
    rm -rfv ${prefix}/include/colib
    rm -rfv ${prefix}/include/iulib
    rm -rfv ${prefix}/include/ocropus
    rm -rfv ${prefix}/include/ocrorast
    rm -rfv ${prefix}/include/ocrolseg
    rm -rfv ${prefix}/include/ocrofst
    rm -rfv ${prefix}/lib/libiulib*
    rm -rfv ${prefix}/lib/libocropus*
    rm -rfv ${prefix}/lib/libocrorast*
    rm -rfv ${prefix}/lib/libocrofst*
    rm -rfv ${prefix}/lib/python*/dist-packages/_ocro*.so
    rm -rfv ${prefix}/lib/python*/dist-packages/ocro*
}

cmd=$1
shift
echo === $cmd
case "$cmd" in
hg)
    doHg "$@"
    ;;

clone)
    doClone "$@"
    ;;

pull)
    doPull "$@"
    ;;

push)
    doPush "$@"
    ;;

commit)
    doCommit "$@"
    ;;

packages)
    doPackages $@
    ;;

cleanup)
    doCleanup
    ;;

install)
    doInstall $@
    ;;

dl)
    doDownload
    ;;

uninstall)
    doUninstall
    ;;

*)
    echo "unknown command:" $1
    echo "ocroinst {hg|clone|pull|push|commit|packages|cleanup|install|dl|uninstall}"
    exit 1
    ;;
esac

